import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    products: [
      { name: '马云', price: '200' },
      { name: '马化腾', price: '160' },
      { name: '马冬梅', price: '20' },
      { name: '马蓉', price: '10' }
    ]

  },
  //在触发事件的时候用mutations
  mutations: {
      reducePrice:(state)=>{
        state.products.forEach(product =>{
          product.price -=1;
        })
      }

  },
  //action提交的是mutations，而不是直接的更改状态。
  actions: {
    
  },
  getters: {
    saleProducts:(state)=>{
      var saleProducts =state.products.map(product => {
        return {
          name: '**' + product.name + '**',
          price: product.price / 2
        };
      });
      return saleProducts;
    }
  },

})
